﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Mani.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;

public partial class Accounts : BasePage {
    
    public override string ActionName {
        get {
            return "vw_accounts";
        }
    }

    private IInternalAccountService internalAccountService;
    public IInternalAccountService InternalAccountService
    {
        get { return internalAccountService; }
        set { internalAccountService = value; }
    }
    static int numberDiv = 0;
    protected void btnCreate_Click( object sender, EventArgs e ) {
        try {
            InternalAccount account = null;

            if (String.IsNullOrEmpty(txtAccountId.Text)) {
                account = new InternalAccount();
                account.CreateDate = DateTime.Now;
            }
            else {
                account = internalAccountService.GetById(Convert.ToInt32(txtAccountId.Text));
            }

            account.Description = txtDescription.Text;
            account.Name = txtAccountName.Text;
            internalAccountService.SaveOrUpadate(account);

            UIHelper.ClientAlert(this.Page, "تغییرات با موفقیت ذخیره شد" );
            bindGrid();
            ResetForm();
        }
        catch {
            throw;
        }
    }

    public void bindGrid()
    {
        gvAccounts.DataSource = InternalAccountService.FindAllAccounts();
        gvAccounts.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        bindGrid();
    }

    private void ResetForm() {
        txtAccountId.Text = "";
        txtAccountName.Text = "";
        txtDescription.Text = "";
    }

    protected void gvAccounts_RowDeleting(object sender, GridViewDeleteEventArgs e) {
        int Id = Convert.ToInt32(gvAccounts.DataKeys[e.RowIndex].Value);
        internalAccountService.DeleteAccount(Id);
        bindGrid();
        ResetForm();
    }

    protected void gvAccounts_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
        int Id = Convert.ToInt32(gvAccounts.DataKeys[e.NewSelectedIndex].Value);

        InternalAccount account = internalAccountService.GetById(Id);
        txtAccountId.Text = account.Id.ToString();
        txtAccountName.Text = account.Name;
        txtDescription.Text = account.Description;
    }
    protected void gvAccounts_Sorting(object sender, GridViewSortEventArgs e)
    {
        bool dir;
        if ((numberDiv % 2) == 0)
            dir = true;
        else
            dir = false;
        numberDiv++;
        gvAccounts.DataSource = InternalAccountService.SortInternalAccount(e.SortExpression, dir);
        gvAccounts.DataBind();
    }
}
